package home_swiper

import (
	"hft/Database/admin/home_swiper"
	home_serve "hft/Http/Services/home_swiper"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type HomeSwiper struct{}

// 添加轮播图
func (*HomeSwiper) AddSwiper(c *gin.Context) {
	var data []*home_swiper.HomeSwiper
	c.ShouldBindJSON(&data)

	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}
	err = home_serve.HomeSwiperService.AddSwiper(data, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

// 获取轮播图
func (*HomeSwiper) List(c *gin.Context) {
	cateType, _ := strconv.Atoi(c.DefaultQuery("type", "0"))
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}
	list, err := home_serve.HomeSwiperService.List(cateType, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.OkWithData(list, c)
}

var HomeSwiperController = new(HomeSwiper)
